package com.gitee.mrj.aiboot.core.sf.generators.type;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.type.AlainInteger;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class AlainIntegerGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        AlainInteger alainField = AnnotatedElementUtils.findMergedAnnotation(field, AlainInteger.class);
        ObjectNode schema = node.getSchema();
        node.getUi().put("widget", SFSchemaType.INTEGER.toString());
        putInt(schema, "maximum", alainField.maximum());
        putInt(schema, "minimum", alainField.minimum());

        if (StringUtils.isNotBlank(alainField.defaultValue())) {
            schema.put("defaultValue", Integer.parseInt(alainField.defaultValue()));
        }
        putInt(schema, "multipleOf", alainField.multipleOf());
        schema.put("type", SFSchemaType.INTEGER.toString());
    }

    @Override
    public Class[] supports() {
        return new Class[]{AlainInteger.class};
    }
}
